﻿using CarparkIMS_Model;
using Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CarparkBusiness
{
    public partial class BackgroundServiceStatus : IBackgroundServiceStatus
    {  
        public bool IsStopped { get; set; } = false;
        public DateTime StoppedOn { get; set; } = new DateTime(1970,1,1,0,0,0);

        public static BackgroundServiceStatus Get()
        {
            string jsonFileName = "BackgroundServiceStatus.json";
            string baseDirectoryPath = AppContext.BaseDirectory; //System.IO.Path.GetFullPath("./"); 
            string pathFileName = Path.Combine(baseDirectoryPath, jsonFileName);
            try
            {
                if (File.Exists(pathFileName))
                {
                    string json = CommonBase.ReadConfigJson(pathFileName);
                    BackgroundServiceStatus backgroundServiceStatus = JsonConvert.DeserializeObject<BackgroundServiceStatus>(json);
                    return backgroundServiceStatus;
                }
                else
                {
                    return new BackgroundServiceStatus();
                }
            }
            catch
            {
                return new BackgroundServiceStatus();
            }
        }
        public static bool Save(BackgroundServiceStatus setting)
        {
            string jsonFileName = "BackgroundServiceStatus.json";
            string baseDirectoryPath = AppContext.BaseDirectory;  //System.IO.Path.GetFullPath("./");
            string pathFileName = Path.Combine(baseDirectoryPath, jsonFileName);
            if(File.Exists(pathFileName))
            {
                File.Delete(pathFileName);
            }
            string json = JsonConvert.SerializeObject(setting); 
            bool result = CommonBase.SaveDataJson(json, pathFileName); 
            return result;
        }
    }

    public interface IBackgroundServiceStatus
    {
        public bool IsStopped { get; set; }
        public DateTime StoppedOn { get; set; } 
    }
}
